package com.suning.sawp.service.impl.airCondition;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.suning.framework.ump.client.SimpleMsg;
import com.suning.nsf.sale.biz.posts.UserCommonService;
import com.suning.nsf.sale.biz.posts.dto.ResponseDTO;
import com.suning.rsf.spring.Reference;
import com.suning.sawp.constants.ConfigConstants;
import com.suning.sawp.constants.ErrorCodeConstants;
import com.suning.sawp.dto.aircondition.AirConditionConstants;
import com.suning.sawp.dto.aircondition.AirConditionRequest;
import com.suning.sawp.dto.aircondition.MyCustRequest;
import com.suning.sawp.dto.aircondition.MyCustResult;
import com.suning.sawp.dto.aircondition.RoomType;
import com.suning.sawp.dto.aircondition.SchemeComm;
import com.suning.sawp.dto.aircondition.SchemeInfo;
import com.suning.sawp.dto.aircondition.SchemePlace;
import com.suning.sawp.dto.aircondition.SchemeResult;
import com.suning.sawp.dto.aircondition.SchemeTemplet;
import com.suning.sawp.dto.aircondition.SchemeTempletHouse;
import com.suning.sawp.dto.aircondition.SchemeTempletInfo;
import com.suning.sawp.dto.aircondition.StoreVo;
import com.suning.sawp.dto.common.ReturnMsg;
import com.suning.sawp.intf.airCondition.AirConditionService;
import com.suning.sawp.po.airCondition.AirCondition;
import com.suning.sawp.po.airCondition.AirConditionComm;
import com.suning.sawp.po.airCondition.AirConditionTemplet;
import com.suning.sawp.po.airCondition.AirConditionTempletComm;
import com.suning.sawp.service.impl.dao.AirConditionDaoService;
import com.suning.sawp.service.util.AESUtils;
import com.suning.sawp.service.util.SCMConfigUtil;

/**
 * 
 * 系统管理服务类<br>
 * 〈功能详细描述〉
 *
 * @author 14062606
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@Service
public class AirConditionServiceImpl implements AirConditionService {

    private static final Logger LOGGER = LoggerFactory.getLogger(AirConditionServiceImpl.class);

    @Autowired
    AirConditionDaoService airConditionDaoService;

    @Reference(contractName = "com.suning.nsf.sale.biz.posts.UserCommonService", implCode = "default")
    private UserCommonService userCommonService;

    /*
     * 当type是4时表示门店编码
     */
    private final String TYPE = "4";

    @Override
    public ReturnMsg<List<SchemeResult>> querySchemeForCust(int index, int size, String storeCode, String mobileNum) {
        ReturnMsg<List<SchemeResult>> returnMsg = new ReturnMsg<List<SchemeResult>>();
        LOGGER.info(" AirConditionServiceImpl querySchemeForCust index:" + index + ", size:" + size + ", mobileNum:"
                + mobileNum + ", storeCode:" + storeCode);

        String cityId = getCityIdByStoreCode(storeCode);

        if (StringUtils.isBlank(cityId)) {
            LOGGER.error(" AirConditionServiceImpl querySchemeForCust cityId is blank index:" + index + ", size:"
                    + size + ", mobileNum:" + mobileNum + ", storeCode:" + storeCode);
            returnMsg.setError(ErrorCodeConstants.ERR_0028);
            return returnMsg;
        }

        List<AirCondition> daoList = airConditionDaoService.querySchemeForCust(index, size, mobileNum, cityId);
        if (daoList == null) {
            LOGGER.error(" AirConditionServiceImpl querySchemeForCust daoList is null index:" + index + ", size:"
                    + size + ", mobileNum:" + mobileNum + ", storeCode:" + storeCode);
            returnMsg.setError(ErrorCodeConstants.ERR_0028);
            return returnMsg;
        }
        List<SchemeResult> schemeResultList = Lists.newArrayList(Lists.transform(daoList,
                new Function<AirCondition, SchemeResult>() {
                    @Override
                    public SchemeResult apply(AirCondition input) {
                        SchemeResult schemeResult = new SchemeResult();
                        try {
                            BeanUtils.copyProperties(schemeResult, input);
                        } catch (IllegalAccessException e) {
                            LOGGER.error("querySchemeForCust IllegalAccessException", e);
                            return schemeResult;
                        } catch (InvocationTargetException e) {
                            LOGGER.error("querySchemeForCust InvocationTargetException", e);
                            return schemeResult;
                        }
                        return schemeResult;
                    }
                }));

        returnMsg.setData(schemeResultList);
        return returnMsg;
    }

    private String getCityIdByStoreCode(String storeCode) {
        String cityId = "";
        ResponseDTO<Object> cityResult;
        try {
            cityResult = userCommonService.findCityAreaInfoByStoreCode(storeCode, TYPE);
            List<StoreVo> stores = cityResult.getResult() == null ? Collections.<StoreVo> emptyList() : JSON
                    .parseObject(cityResult.getResult().toString(), new TypeReference<List<StoreVo>>() {
                    });

            if (CollectionUtils.isNotEmpty(stores)) {
                StoreVo storeVo = stores.get(0);
                cityId = storeVo.getCityCode();
            }
        } catch (Exception e) {
            LOGGER.error("getCityIdByStoreCode error", e);
        }

        return cityId;
    }

    @Override
    public ReturnMsg<SchemeInfo> querySchemeInfo(String schemeCode) {

        ReturnMsg<SchemeInfo> returnMsg = new ReturnMsg<SchemeInfo>();
        SchemeInfo schemeInfo = new SchemeInfo();
        LOGGER.info(" AirConditionServiceImpl querySchemeInfo schemeCode " + schemeCode);
        if (StringUtils.isBlank(schemeCode)) {
            LOGGER.error(" AirConditionServiceImpl querySchemeInfo schemeCode is blank schemeCode:" + schemeCode);
            returnMsg.setError(ErrorCodeConstants.ERR_0028);
            return returnMsg;
        }

        AirCondition airCondition = airConditionDaoService.querySchemeByID(schemeCode);
        if (airCondition == null) {
            LOGGER.error(" AirConditionServiceImpl querySchemeInfo airCondition is null schemeCode:" + schemeCode);
            returnMsg.setError(ErrorCodeConstants.ERR_0028);
            return returnMsg;
        }

        try {
            BeanUtils.copyProperties(schemeInfo, airCondition);
            if (StringUtils.isNotBlank(schemeInfo.getCustNum())) {
                schemeInfo.setCustNum(AESUtils.encrypt(schemeInfo.getCustNum(), ConfigConstants.AES_KEY));
            }
            if (StringUtils.isNotBlank(schemeInfo.getCustNumBack())) {
                schemeInfo.setCustNumBack(AESUtils.encrypt(schemeInfo.getCustNumBack(), ConfigConstants.AES_KEY));
            }

            List<AirConditionComm> airConditionCommList = airConditionDaoService.querySchemeCommList(schemeCode);

            if (CollectionUtils.isNotEmpty(airConditionCommList)) {
                Map<String, SchemePlace> schemePlaceMap = new HashMap<String, SchemePlace>();
                for (AirConditionComm item : airConditionCommList) {
                    if (StringUtils.isBlank(item.getPositionCode())) {
                        continue;
                    }
                    if (!schemePlaceMap.containsKey(item.getPositionCode())) {
                        SchemePlace newSchemePlace = new SchemePlace();
                        List<SchemeComm> newCommList = new ArrayList<SchemeComm>();
                        newSchemePlace.setCommList(newCommList);
                        newSchemePlace.setPositionCode(item.getPositionCode());
                        newSchemePlace.setPositionName(item.getPositionName());
                        schemePlaceMap.put(item.getPositionCode(), newSchemePlace);
                    }
                    SchemePlace schemePlace = schemePlaceMap.get(item.getPositionCode());
                    List<SchemeComm> commList = schemePlace.getCommList();
                    SchemeComm detail = new SchemeComm();
                    BeanUtils.copyProperties(detail, item);
                    commList.add(detail);
                }
                List<SchemePlace> schemePlaceList = new ArrayList<SchemePlace>(schemePlaceMap.values());
                Collections.sort(schemePlaceList, new Comparator<SchemePlace>(){  
                    
                    /*  
                     * int compare(SchemePlace o1, SchemePlace o2) 返回一个基本类型的整型，  
                     * 返回负数表示：o1 小于o2，  
                     * 返回0 表示：o1和o2相等，  
                     * 返回正数表示：o1大于o2。  
                     */  
                    public int compare(SchemePlace o1, SchemePlace o2) {  
                        
                        //进行升序排列  
                        if(  o1.getPositionCode().compareTo(o2.getPositionCode()) == 1){  
                            return 1;  
                        }  
                        if(  o1.getPositionCode().compareTo(o2.getPositionCode()) == -1){  
                            return -1;  
                        }  
                        return 0;  
                    }  
                });   
                schemeInfo.setPlaceList(schemePlaceList);
            }

        } catch (IllegalAccessException e) {
            LOGGER.error(" AirConditionServiceImpl querySchemeInfo IllegalAccessExceptionl schemeCode:" + schemeCode, e);
            returnMsg.setError(ErrorCodeConstants.ERR_0028);
            return returnMsg;
        } catch (InvocationTargetException e) {
            LOGGER.error(" AirConditionServiceImpl querySchemeInfo InvocationTargetException schemeCode:" + schemeCode,
                    e);
            returnMsg.setError(ErrorCodeConstants.ERR_0028);
            return returnMsg;
        }
        returnMsg.setData(schemeInfo);
        return returnMsg;
    }

    @Override
    public ReturnMsg<List<SchemeTempletHouse>> querySchemeTemplet(String branchCode, String resouce) {
        ReturnMsg<List<SchemeTempletHouse>> returnMsg = new ReturnMsg<List<SchemeTempletHouse>>();
        LOGGER.info(" AirConditionServiceImpl querySchemeTemplet branchCode " + branchCode);

        List<AirConditionTemplet> templetlist = airConditionDaoService.querySchemeTemplet(branchCode);
        if (templetlist == null) {
            LOGGER.error(" AirConditionServiceImpl querySchemeTemplet   templetlist is null branchCode:" + branchCode);
            returnMsg.setError(ErrorCodeConstants.ERR_0028);
            return returnMsg;
        }

        // 如果为空则是客户端接口需要其他模板
        if (StringUtils.isBlank(resouce)) {
            setOtherContent(templetlist, branchCode);
        }

        Map<String, SchemeTempletHouse> templetMap = new HashMap<String, SchemeTempletHouse>();
        for (AirConditionTemplet item : templetlist) {
            if (StringUtils.isBlank(item.getHouseType())) {
                continue;
            }
            if (!templetMap.containsKey(item.getHouseType())) {
                SchemeTempletHouse newSchemeTempletHouse = new SchemeTempletHouse();
                List<SchemeTemplet> newTempletList = new ArrayList<SchemeTemplet>();
                newSchemeTempletHouse.setTempletList(newTempletList);
                newSchemeTempletHouse.setHouseType(item.getHouseType());
                newSchemeTempletHouse.setHouseTypeName(item.getHouseTypeName());
                templetMap.put(item.getHouseType(), newSchemeTempletHouse);
            }
            SchemeTempletHouse schemeTempletHouse = templetMap.get(item.getHouseType());
            List<SchemeTemplet> schemeTempletList = schemeTempletHouse.getTempletList();
            SchemeTemplet detail = new SchemeTemplet();
            detail.setTempletId(item.getTempletId());
            detail.setTempletName(item.getTempletName());
            detail.setTempletType(item.getTempletType());
            schemeTempletList.add(detail);
        }
        List<SchemeTempletHouse> schemeTempletHouseList = new ArrayList<SchemeTempletHouse>(templetMap.values());
        
        Collections.sort(schemeTempletHouseList, new Comparator<SchemeTempletHouse>(){  
            
            /*  
             * int compare(SchemePlace o1, SchemePlace o2) 返回一个基本类型的整型，  
             * 返回负数表示：o1 小于o2，  
             * 返回0 表示：o1和o2相等，  
             * 返回正数表示：o1大于o2。  
             */  
            public int compare(SchemeTempletHouse o1, SchemeTempletHouse o2) {  
                
                //进行升序排列  
                if(  o1.getHouseType().compareTo(o2.getHouseType()) > 0){  
                    return 1;  
                }  
                if(  o1.getHouseType().compareTo(o2.getHouseType()) < 0){  
                    return -1;  
                }  
                return 0;  
            }  
        });   
        
        
        returnMsg.setData(schemeTempletHouseList);
        return returnMsg;
    }

    private void setOtherContent(List<AirConditionTemplet> templetlist, String branchCode) {
        // 如果查询结果为空，说明对应的分公司没有创建模板，所以对对应分公司的每一个户型中加入其它选项，并配置对应的户型
        if (templetlist != null) {

            List<String> houseTypeList = AirConditionConstants.HOUSE_TYPE_LIST;

            for (String item : houseTypeList) {
                // List<RoomType> roomList = AirConditionConstants.HOUSE_ROOMNAME_MAP.get(item);
                AirConditionTemplet detail = new AirConditionTemplet();
                detail.setBranchId(branchCode);
                detail.setHouseType(item);
                detail.setHouseTypeName(AirConditionConstants.HOUSE_TYPE_MAP.get(item));
                detail.setTempletName("其他");
                detail.setTempletId(AirConditionConstants.AIRCON.QITA);
                templetlist.add(detail);
            }
        }
    }

    @Override
    public ReturnMsg<SchemeTempletInfo> queryTempletById(String templetId, String houseType) {
        ReturnMsg<SchemeTempletInfo> returnMsg = new ReturnMsg<SchemeTempletInfo>();
        LOGGER.info(" AirConditionServiceImpl queryTempletById templetId " + templetId);
        SchemeTempletInfo schemeTempletInfo = new SchemeTempletInfo();
        // 如果模板为其他
        if (AirConditionConstants.AIRCON.QITA.equals(templetId)) {
            List<SchemePlace> placeList = new ArrayList<SchemePlace>();
            SchemePlace schemePlace1 = new SchemePlace();
            schemePlace1.setPositionCode(AirConditionConstants.AIRCON.SHINEIJI);
            schemePlace1.setPositionName(AirConditionConstants.PLACE_MAP.get(AirConditionConstants.AIRCON.SHINEIJI));

            List<SchemeComm> commList = new ArrayList<SchemeComm>();
            List<RoomType> roomTypeList = AirConditionConstants.HOUSE_ROOMNAME_MAP.get(houseType);
            for (RoomType item : roomTypeList) {
                SchemeComm schemeComm = new SchemeComm();
                schemeComm.setRoomCode(item.getRoomType());
                schemeComm.setRoomName(item.getRoomName());
                schemeComm.setCommCode("");
                schemeComm.setCommName("");
                schemeComm.setPrice("");
                schemeComm.setSize("");
                commList.add(schemeComm);
            }
            schemePlace1.setCommList(commList);

            SchemePlace schemePlace2 = new SchemePlace();
            schemePlace2.setPositionCode(AirConditionConstants.AIRCON.SHIWAIJI);
            schemePlace2.setPositionName(AirConditionConstants.PLACE_MAP.get(AirConditionConstants.AIRCON.SHIWAIJI));
            List<SchemeComm> commList2 = new ArrayList<SchemeComm>();
            SchemeComm schemeComm2 = new SchemeComm();
            schemeComm2.setRoomCode(AirConditionConstants.AIRCON.SHIWAIJI_ROOM_TYPE);
            schemeComm2.setRoomName(AirConditionConstants.AIRCON.SHIWAIJI_ROOM_NAME);
            schemeComm2.setCommCode("");
            schemeComm2.setCommName("");
            schemeComm2.setPrice("");
            schemeComm2.setSize("");
            commList2.add(schemeComm2);
            schemePlace2.setCommList(commList2);

            placeList.add(schemePlace1);
            placeList.add(schemePlace2);
            schemeTempletInfo.setPlaceList(placeList);
            schemeTempletInfo.setBrandCode(AirConditionConstants.BRANDCON.DAJIN);
            schemeTempletInfo.setBrandName(AirConditionConstants.BRAND_MAP.get(AirConditionConstants.BRANDCON.DAJIN));
            schemeTempletInfo.setShoppingCode(AirConditionConstants.AIRCON.DANJI_SHOP);
            returnMsg.setData(schemeTempletInfo);
            return returnMsg;
        }
        AirConditionTemplet airConditionTemplet = airConditionDaoService.queryTempletById(templetId);
        if (airConditionTemplet == null) {
            LOGGER.error(" AirConditionServiceImpl queryTempletById airConditionTemplet is null templetId:" + templetId);
            returnMsg.setError(ErrorCodeConstants.ERR_0028);
            return returnMsg;
        }

        try {
            BeanUtils.copyProperties(schemeTempletInfo, airConditionTemplet);
            List<AirConditionTempletComm> commlist = airConditionDaoService.queryTempletCommById(templetId);

            if (commlist == null) {
                LOGGER.error(" AirConditionServiceImpl queryTempletById commlist is null templetId:" + templetId);
                returnMsg.setError(ErrorCodeConstants.ERR_0028);
                return returnMsg;
            }

            Map<String, SchemePlace> schemePlaceMap = new HashMap<String, SchemePlace>();
            for (AirConditionTempletComm item : commlist) {
                if (StringUtils.isBlank(item.getPositionCode())) {
                    continue;
                }
                if (!schemePlaceMap.containsKey(item.getPositionCode())) {
                    SchemePlace newSchemePlace = new SchemePlace();
                    List<SchemeComm> newCommList = new ArrayList<SchemeComm>();
                    newSchemePlace.setCommList(newCommList);
                    newSchemePlace.setPositionCode(item.getPositionCode());
                    newSchemePlace.setPositionName(item.getPositionName());
                    schemePlaceMap.put(item.getPositionCode(), newSchemePlace);
                }
                SchemePlace schemePlace = schemePlaceMap.get(item.getPositionCode());
                List<SchemeComm> commList = schemePlace.getCommList();
                SchemeComm detail = new SchemeComm();
                BeanUtils.copyProperties(detail, item);
                commList.add(detail);
            }
            List<SchemePlace> schemePlaceList = new ArrayList<SchemePlace>(schemePlaceMap.values());
            Collections.sort(schemePlaceList, new Comparator<SchemePlace>(){  
                
                /*  
                 * int compare(SchemePlace o1, SchemePlace o2) 返回一个基本类型的整型，  
                 * 返回负数表示：o1 小于o2，  
                 * 返回0 表示：o1和o2相等，  
                 * 返回正数表示：o1大于o2。  
                 */  
                public int compare(SchemePlace o1, SchemePlace o2) {  
                    
                    //进行升序排列  
                    if(  o1.getPositionCode().compareTo(o2.getPositionCode()) == 1){  
                        return 1;  
                    }  
                    if(  o1.getPositionCode().compareTo(o2.getPositionCode()) == -1){  
                        return -1;  
                    }  
                    return 0;  
                }  
            });   
            
            schemeTempletInfo.setPlaceList(schemePlaceList);
            returnMsg.setData(schemeTempletInfo);
        } catch (IllegalAccessException e) {
            LOGGER.error(" AirConditionServiceImpl IllegalAccessException templetId:" + templetId, e);
            returnMsg.setError(ErrorCodeConstants.ERR_0028);
        } catch (InvocationTargetException e) {
            LOGGER.error(" AirConditionServiceImpl InvocationTargetException templetId :" + templetId, e);
            returnMsg.setError(ErrorCodeConstants.ERR_0028);
        }
        return returnMsg;
    }

    @Override
    public ReturnMsg<List<MyCustResult>> queryMyCust(String staffId, MyCustRequest myCustRequest) {
        ReturnMsg<List<MyCustResult>> returnMsg = new ReturnMsg<List<MyCustResult>>();
        LOGGER.info(" AirConditionServiceImpl queryMyCust staffId " + staffId);

        List<AirCondition> daoList = airConditionDaoService.queryMyCust(myCustRequest, staffId);

        if (daoList == null) {
            LOGGER.error(" AirConditionServiceImpl queryMyCust airConditionTemplet is null staffId:" + staffId);
            returnMsg.setError(ErrorCodeConstants.ERR_0028);
            return returnMsg;
        }
        if (CollectionUtils.isNotEmpty(daoList)) {
            List<MyCustResult> schemeResultList = Lists.newArrayList(Lists.transform(daoList,
                    new Function<AirCondition, MyCustResult>() {
                        @Override
                        public MyCustResult apply(AirCondition input) {
                            MyCustResult myCustResult = new MyCustResult();
                            try {
                                BeanUtils.copyProperties(myCustResult, input);
                            } catch (IllegalAccessException e) {
                                LOGGER.error("querySchemeForCust IllegalAccessException", e);
                            } catch (InvocationTargetException e) {
                                LOGGER.error("querySchemeForCust InvocationTargetException", e);
                            }
                            return myCustResult;
                        }
                    }));
            returnMsg.setData(schemeResultList);
        } else {
            returnMsg.setData(new ArrayList<MyCustResult>());
        }
        return returnMsg;
    }

    @Override
    public ReturnMsg<String> deleteRecord(String staffId, String schemeCode) {
        ReturnMsg<String> returnMsg = new ReturnMsg<String>();
        LOGGER.info(" AirConditionServiceImpl deleteRecord staffId:{},schemeCode:{}", staffId, schemeCode);
        // 软删除对应店员对应客户的方案资料
        boolean result = airConditionDaoService.deleteRecord(staffId, schemeCode);
        if (!result) {
            LOGGER.error(" AirConditionServiceImpl deleteRecord fail staffId:" + staffId);
            returnMsg.setError(ErrorCodeConstants.ERR_0002);
        }
        return returnMsg;
    }

    @Override
    public ReturnMsg<String> submitCustMade(AirConditionRequest airConditionRequest) {
        ReturnMsg<String> returnMsg = new ReturnMsg<String>();
        LOGGER.info(" AirConditionServiceImpl submitCustMade staffId:{},airCondition:{}", airConditionRequest);
        AirCondition airCondition = new AirCondition();
        try {
            BeanUtils.copyProperties(airCondition, airConditionRequest);
            String cityId = getCityIdByStoreCode(airConditionRequest.getStoreCode());
            if (StringUtils.isBlank(cityId)) {
                LOGGER.error(" AirConditionServiceImpl submitCustMade fail cityid is blank");
                returnMsg.setError(ErrorCodeConstants.ERR_0002);
                return returnMsg;
            }
            airCondition.setCityCode(cityId);
            boolean result = airConditionDaoService.submitCustMade(airCondition);
            if (!result) {
                LOGGER.error(" AirConditionServiceImpl submitCustMade fail ");
                returnMsg.setError(ErrorCodeConstants.ERR_0002);
            }
        } catch (IllegalAccessException e) {
            LOGGER.error(" AirConditionServiceImpl IllegalAccessException :", e);
            returnMsg.setError(ErrorCodeConstants.ERR_0002);
        } catch (InvocationTargetException e) {
            LOGGER.error(" AirConditionServiceImpl InvocationTargetException :", e);
            returnMsg.setError(ErrorCodeConstants.ERR_0002);
        }
        return returnMsg;
    }

    @Override
    public ReturnMsg<String> isOverLimit(String commCode, String custNum, String price, String storeCode,String staffId) {
        ReturnMsg<String> returnMsg = new ReturnMsg<String>();
        LOGGER.info(" AirConditionServiceImpl isOverLimit storeCode:{},commCode:{},price:{}", storeCode, commCode,
                price);
        String cityId = getCityIdByStoreCode(storeCode);
        if (StringUtils.isBlank(cityId)) {
            LOGGER.error(" AirConditionServiceImpl isOverLimit fail cityid is blank");
            returnMsg.setError(ErrorCodeConstants.ERR_0002);
            return returnMsg;
        }
        Double result = airConditionDaoService.isOverLimit(commCode, custNum, cityId,staffId);
        if (result == null) {
            // 如果查询不到结果默认为不超过限价
            returnMsg.setData("0");
            return returnMsg;
        }
        if (Double.valueOf(price) < result) {
            // 如果提交价格比客户最低价格低
            returnMsg.setData("1");
        } else {
            returnMsg.setData("0");
        }
        return returnMsg;
    }

    @Override
    @Transactional
    public ReturnMsg<String> submitScheme(SchemeInfo schemeInfo, String storeCode) {
        ReturnMsg<String> returnMsg = new ReturnMsg<String>();
        LOGGER.info(" AirConditionServiceImpl submitScheme schemeInfo : " + schemeInfo);
        AirCondition airCondition = new AirCondition();
        try {
            BeanUtils.copyProperties(airCondition, schemeInfo);

            String cityId = getCityIdByStoreCode(storeCode);
            if (StringUtils.isBlank(cityId)) {
                LOGGER.error(" AirConditionServiceImpl submitScheme fail cityid is blank");
                returnMsg.setError(ErrorCodeConstants.ERR_0002);
                return returnMsg;
            }
            airCondition.setCityCode(cityId);

            String schemeResult = airConditionDaoService.submitScheme(airCondition);
            if (StringUtils.isBlank(schemeResult)) {
                LOGGER.error(" AirConditionServiceImpl submitScheme fail ");
                returnMsg.setError(ErrorCodeConstants.ERR_0002);
                return returnMsg;
            }

            List<AirConditionComm> commList = new ArrayList<AirConditionComm>();
            List<SchemePlace> schemePlaceList = schemeInfo.getPlaceList();
            if (CollectionUtils.isNotEmpty(schemePlaceList)) {
                for (SchemePlace item : schemePlaceList) {
                    List<SchemeComm> commItemList = item.getCommList();
                    if (CollectionUtils.isNotEmpty(commItemList)) {
                        for (SchemeComm commItem : commItemList) {
                            AirConditionComm commDto = new AirConditionComm();
                            BeanUtils.copyProperties(commDto, commItem);
                            commDto.setPositionCode(item.getPositionCode());
                            commDto.setPositionName(item.getPositionName());
                            commDto.setSchemeCode(schemeResult);
                            commList.add(commDto);
                        }
                    }
                }
            }
            if (CollectionUtils.isNotEmpty(commList)) {
                boolean schemeCommResult = airConditionDaoService.submitSchemeComm(commList);
                if (!schemeCommResult) {
                    LOGGER.error(" AirConditionServiceImpl submitScheme fail ");
                    returnMsg.setError(ErrorCodeConstants.ERR_0002);
                }
            }

        } catch (IllegalAccessException e) {
            LOGGER.error(" AirConditionServiceImpl submitScheme IllegalAccessException :", e);
            returnMsg.setError(ErrorCodeConstants.ERR_0002);
        } catch (InvocationTargetException e) {
            LOGGER.error(" AirConditionServiceImpl submitScheme InvocationTargetException :", e);
            returnMsg.setError(ErrorCodeConstants.ERR_0002);
        }

        return returnMsg;
    }

    @Override
    public ReturnMsg<String> sendSms(String custNum, String staffoMobile, String storeName, String staffName,
            String orderTime, String type) {

        String content;
        if (AirConditionConstants.AIRCON.SMS_ORDER_TYPE.equals(type)) {

            String orderDefultModel = SCMConfigUtil.getConfig(ConfigConstants.AIRCONDITION_ORDER_MSG_MODEL,
                    ConfigConstants.AIRCONDITION_ORDER_DEFAULT_MODEL);
            content = String.format(orderDefultModel, orderTime, storeName, staffName, staffoMobile);
        } else {
            String defultModel = SCMConfigUtil.getConfig(ConfigConstants.AIRCONDITION_MSG_MODEL,
                    ConfigConstants.AIRCONDITION_DEFAULT_MODEL);
            content = String.format(defultModel, storeName, staffName, staffoMobile);
        }
        ReturnMsg<String> returnMsg = new ReturnMsg<String>();
        LOGGER.info(" AirConditionServiceImpl isOverLimit sendSms content : " + content);
        boolean result = false;
        try {
            result = SimpleMsg.send(custNum, content);
        } catch (Exception e) {
            LOGGER.error(" AirConditionServiceImpl sendSms Exception for  :" + content, e);
        }

        if (!result) {
            LOGGER.error(" AirConditionServiceImpl sendSms error for  :" + content);
            returnMsg.setError(ErrorCodeConstants.ERR_0002);
        }
        return returnMsg;
    }

    @Override
    @Transactional
    public ReturnMsg<String> updateScheme(SchemeInfo schemeInfo) {

        ReturnMsg<String> returnMsg = new ReturnMsg<String>();
        LOGGER.info(" AirConditionServiceImpl updateScheme schemeInfo : " + schemeInfo);

        // 更新方案
        boolean schemeResult = airConditionDaoService.updateScheme(schemeInfo);
        // 如果更新成功
        if (schemeResult) {
            try {
                // 先删除对应方案下商品
                boolean deleteResult = airConditionDaoService.deleteSchemeComm(schemeInfo.getSchemeCode());
                if (deleteResult) {
                    // 遍历出对应出对应的商品
                    List<AirConditionComm> commList = new ArrayList<AirConditionComm>();
                    List<SchemePlace> schemePlaceList = schemeInfo.getPlaceList();
                    if (CollectionUtils.isNotEmpty(schemePlaceList)) {
                        for (SchemePlace item : schemePlaceList) {
                            List<SchemeComm> commItemList = item.getCommList();
                            if (CollectionUtils.isNotEmpty(commItemList)) {
                                for (SchemeComm commItem : commItemList) {
                                    AirConditionComm commDto = new AirConditionComm();
                                    BeanUtils.copyProperties(commDto, commItem);
                                    commDto.setPositionCode(item.getPositionCode());
                                    commDto.setPositionName(item.getPositionName());
                                    commDto.setSchemeCode(schemeInfo.getSchemeCode());
                                    commList.add(commDto);
                                }
                            }
                        }
                    }
                    if (CollectionUtils.isNotEmpty(commList)) {
                        boolean schemeCommResult = airConditionDaoService.submitSchemeComm(commList);
                        if (!schemeCommResult) {
                            LOGGER.error(" AirConditionServiceImpl submitScheme fail ");
                            returnMsg.setError(ErrorCodeConstants.ERR_0002);
                        }
                    }
                } else {
                    LOGGER.error(" AirConditionServiceImpl deleteSchemeComm fail schemeInfo : " + schemeInfo);
                    returnMsg.setError(ErrorCodeConstants.ERR_0002);
                }

            } catch (IllegalAccessException e) {
                LOGGER.error(" AirConditionServiceImpl updateScheme IllegalAccessException :", e);
                returnMsg.setError(ErrorCodeConstants.ERR_0002);
            } catch (InvocationTargetException e) {
                LOGGER.error(" AirConditionServiceImpl updateScheme InvocationTargetException :", e);
                returnMsg.setError(ErrorCodeConstants.ERR_0002);
            }
        } else {
            LOGGER.error(" AirConditionServiceImpl updateScheme fail schemeInfo : " + schemeInfo);
            returnMsg.setError(ErrorCodeConstants.ERR_0002);
        }
        return returnMsg;

    }

    @Override
    public ReturnMsg<String> submitSchemeTemplet(SchemeTempletInfo schemeTempletInfo, String branchCode) {
        ReturnMsg<String> returnMsg = new ReturnMsg<String>();
        LOGGER.info(" AirConditionServiceImpl submitScheme schemeInfo : " + schemeTempletInfo);
        AirConditionTemplet airCondition = new AirConditionTemplet();
        try {
            BeanUtils.copyProperties(airCondition, schemeTempletInfo);
            airCondition.setBranchId(branchCode);

            String schemeResult = airConditionDaoService.submitSchemeTemplet(airCondition);
            if (StringUtils.isBlank(schemeResult)) {
                LOGGER.error(" AirConditionServiceImpl submitSchemeTemplet fail ");
                returnMsg.setError(ErrorCodeConstants.ERR_0002);
                return returnMsg;
            }

            List<AirConditionTempletComm> commList = new ArrayList<AirConditionTempletComm>();
            List<SchemePlace> schemePlaceList = schemeTempletInfo.getPlaceList();
            if (CollectionUtils.isNotEmpty(schemePlaceList)) {
                for (SchemePlace item : schemePlaceList) {
                    List<SchemeComm> commItemList = item.getCommList();
                    if (CollectionUtils.isNotEmpty(commItemList)) {
                        for (SchemeComm commItem : commItemList) {
                            AirConditionTempletComm commDto = new AirConditionTempletComm();
                            BeanUtils.copyProperties(commDto, commItem);
                            commDto.setPositionCode(item.getPositionCode());
                            commDto.setPositionName(item.getPositionName());
                            commDto.setTempletId(schemeResult);
                            commList.add(commDto);
                        }
                    }
                }
            }
            if (CollectionUtils.isNotEmpty(commList)) {
                boolean schemeCommResult = airConditionDaoService.submitSchemeTempletComm(commList);
                if (!schemeCommResult) {
                    LOGGER.error(" AirConditionServiceImpl submitSchemeTempletComm fail ");
                    returnMsg.setError(ErrorCodeConstants.ERR_0002);
                }
            }
        } catch (IllegalAccessException e) {
            LOGGER.error(" AirConditionServiceImpl submitSchemeTemplet IllegalAccessException :", e);
            returnMsg.setError(ErrorCodeConstants.ERR_0002);
        } catch (InvocationTargetException e) {
            LOGGER.error(" AirConditionServiceImpl submitSchemeTemplet InvocationTargetException :", e);
            returnMsg.setError(ErrorCodeConstants.ERR_0002);
        }

        return returnMsg;
    }

    @Override
    public ReturnMsg<String> updateSchemeTemplet(SchemeTempletInfo schemeTempletInfo, String branchCode) {

        ReturnMsg<String> returnMsg = new ReturnMsg<String>();
        LOGGER.info(" AirConditionServiceImpl updateSchemeTemplet schemeTempletInfo : " + schemeTempletInfo);

        // 更新方案
        boolean schemeResult = airConditionDaoService.updateSchemeTemplet(schemeTempletInfo);
        // 如果更新成功
        if (schemeResult) {
            try {
                // 先删除对应方案下商品
                boolean deleteResult = airConditionDaoService.deleteSchemeTempletComm(schemeTempletInfo.getTempletId());
                if (deleteResult) {
                    // 遍历出对应出对应的商品
                    List<AirConditionTempletComm> commList = new ArrayList<AirConditionTempletComm>();
                    List<SchemePlace> schemePlaceList = schemeTempletInfo.getPlaceList();
                    if (CollectionUtils.isNotEmpty(schemePlaceList)) {
                        for (SchemePlace item : schemePlaceList) {
                            List<SchemeComm> commItemList = item.getCommList();
                            if (CollectionUtils.isNotEmpty(commItemList)) {
                                for (SchemeComm commItem : commItemList) {
                                    AirConditionTempletComm commDto = new AirConditionTempletComm();
                                    BeanUtils.copyProperties(commDto, commItem);
                                    commDto.setPositionCode(item.getPositionCode());
                                    commDto.setPositionName(item.getPositionName());
                                    commDto.setTempletId(schemeTempletInfo.getTempletId());
                                    commList.add(commDto);
                                }
                            }
                        }
                    }
                    if (CollectionUtils.isNotEmpty(commList)) {
                        boolean schemeCommResult = airConditionDaoService.submitSchemeTempletComm(commList);
                        if (!schemeCommResult) {
                            LOGGER.error(" AirConditionServiceImpl submitSchemeTempletComm fail ");
                            returnMsg.setError(ErrorCodeConstants.ERR_0002);
                        }
                    }
                } else {
                    LOGGER.error(" AirConditionServiceImpl deleteSchemeTempletComm fail schemeTempletInfo : "
                            + schemeTempletInfo);
                    returnMsg.setError(ErrorCodeConstants.ERR_0002);
                }

            } catch (IllegalAccessException e) {
                LOGGER.error(" AirConditionServiceImpl updateSchemeTemplet IllegalAccessException :", e);
                returnMsg.setError(ErrorCodeConstants.ERR_0002);
            } catch (InvocationTargetException e) {
                LOGGER.error(" AirConditionServiceImpl updateSchemeTemplet InvocationTargetException :", e);
                returnMsg.setError(ErrorCodeConstants.ERR_0002);
            }
        } else {
            LOGGER.error(" AirConditionServiceImpl updateSchemeTemplet fail schemeTempletInfo : " + schemeTempletInfo);
            returnMsg.setError(ErrorCodeConstants.ERR_0002);
        }
        return returnMsg;
    }
}
